//app.js
var amapFile = require('/libs/amap-wx.js');//如：..­/..­/libs/amap-wx.js
App({
  data:{
    // server:"http://localhost",
     server: "https://www.xieer.net.cn",
    isshouquan:0
  },
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    //   var address = {
    //       longitude:0,
    //       latitude:0,
    //       country:'中国',//国家
    //       province:'山东省',//省
    //       city:'临沂市',//市
    //       district:'河东区',//区
    //       township:'汤头'//街道
    //   }
    //   // console.log(address)
    //   getApp().globalData.address = address;
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // wx.getLocation({
    //   success:function(e){
    //     console.log(JSON.stringify(e))
    //     wx.request({
    //       url:'http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=35.658651,139.745415&output=json&pois=1&ak=O7jtosqrZcivHYFCsjOjo808OHDRSyBu',
    //       success:function(e){
    //         console.log(JSON.stringify(e))
    //       }
    //     })
    //   }
    // })


    var myAmapFun = new amapFile.AMapWX({key:'faeab9701aeb3d950dec0f4007f1afd0'});
    myAmapFun.getRegeo({
      success: function(data){
        //成功回调
        // console.log(JSON.stringify(data))
          var address = {
              longitude:data[0].longitude,//自己当前的经纬度
              latitude:data[0].latitude,//自己当前的经纬度
              otherlon:data[0].longitude,
              otherlat:data[0].latitude,
              country:data[0].regeocodeData.addressComponent.country,//国家
              province:data[0].regeocodeData.addressComponent.province,//省
              city:data[0].regeocodeData.addressComponent.city,//市
              district:data[0].regeocodeData.addressComponent.district,//区
              township:data[0].regeocodeData.addressComponent.township//街道
          }
          // console.log(address)
          getApp().globalData.address = address;
      },
      fail: function(info){
        //失败回调
        console.log(info)

      }
    })
  },
  onLoad:function(e){

  },
  getUserInfo:function(cb){
    var that = this
    console.log(this.globalData.userInfo)
    if(!this.globalData.userInfo){
      console.log('login1')
      typeof cb == "function" && cb(this.globalData.userInfo)
    }else{
      console.log('login')
      //调用登录接口
      wx.login({
        success: function (d) {
          console.log(JSON.stringify(d))
          //登录成功调用接口获取用户信息
          if(d.errMsg=='login:ok'){
            wx.request({
              url: getApp().data.server + '/index.php/Tongcheng/Book/codeToOpanId', 
              data: {
                 code:d.code
              },
              header: {'content-type': 'application/x-www-form-urlencoded'},
              method:'POST',
              dataType:'json',
              success: function(res) {
                  var member = res.data;
                  wx.getSetting({
                  success: function(res){
                    // console.log(res.authSetting['scope.userInfo'])
                    if (res.authSetting['scope.userInfo']) {
                      // 已经授权，可以直接调用 getUserInfo 获取头像昵称
                      //判断是否获取用户信息
                      console.log(member)
                      if(member.ishave == 1){
                        getApp().globalData.userInfo = member;
                        return;
                      }
                      //获取用户信息并提交后台补充用户信息
                      wx.getUserInfo({
                        success: function(res) {
                          console.log(res.userInfo);
                          wx.request({
                            url: getApp().data.server + '/index.php/Tongcheng/Book/buChongMember', 
                            data: {
                               uid:member.uid,
                               userinfo:JSON.stringify(res.userInfo)
                            },
                            header: {'content-type': 'application/x-www-form-urlencoded'},
                            method:'POST',
                            dataType:'json',
                            success: function(res) {
                              getApp().globalData.userInfo=res.data;
                              typeof cb == "function" && cb(getApp().globalData.userInfo);
                            }
                          })
                        }
                      })
                    }else{
                      console.log('未授权')
                    }
                  }
                })
              }
            })
          }else{
            console.log('登录失败，请重新尝试');
            return;
          }
        //   wx.getSetting({
        //   success: function(res){
        //     // console.log(res.authSetting['scope.userInfo'])
        //     if (res.authSetting['scope.userInfo']) {
        //       // 已经授权，可以直接调用 getUserInfo 获取头像昵称
        //       wx.getUserInfo({
        //         success: function(res) {
        //           console.log(res.userInfo);
        //           that.globalData.userInfo=res.userInfo;
        //           typeof cb == "function" && cb(that.globalData.userInfo);
        //         }
        //       })
        //     }else{
        //       that.setData({
        //         isshouquan:1
        //       })
        //     }
        //   }
        // })
      }
    })
    }
  },
  editTabBar: function(){
    var tabbar = this.globalData.tabbar,
        currentPages = getCurrentPages(),
        _this = currentPages[currentPages.length - 1],
        pagePath = _this.__route__;
    (pagePath.indexOf('/') != 0) && (pagePath = '/' + pagePath);
    for(var i in tabbar.list){
      tabbar.list[i].selected = false;
      (tabbar.list[i].pagePath == pagePath) && (tabbar.list[i].selected = true);
    }
    _this.setData({
      tabbar: tabbar
    });
  },
  imageBig:function(e){
    wx.previewImage({
      current: '', // 当前显示图片的http链接
      urls: ['https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1527567438513&di=189dd5a75ca04b8364424b8c53817144&imgtype=0&src=http%3A%2F%2Fa.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F730e0cf3d7ca7bcb6a172486b2096b63f624a82f.jpg',
      'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1527567438513&di=24b1368f9d898ab9d144bab1ffdfb890&imgtype=0&src=http%3A%2F%2Fh.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F91529822720e0cf3b886be520646f21fbe09aa22.jpg'] // 需要预览的图片http链接列表
    })
  },
  addShouCang:function(formData){

    wx.request({
      url: getApp().data.server + '/index.php/Tongcheng/Book/shouCang', 
      data: {
         formData:JSON.stringify(formData),
         uid:getApp().globalData.userInfo.uid
      },
      header: {'content-type': 'application/x-www-form-urlencoded'},
      method:'POST',
      dataType:'json',
      success: function(res) {
        console.log(res.data)
        if(1 == res.data){
          wx.showToast({
            title:'收藏成功',
            icon: 'success',
            duration: 2000
          })
        }else if (2 == res.data){
          wx.showToast({
            title:'已收藏',
            icon: 'fail',
            duration: 2000
          })
        }else{
          wx.showToast({
            title:'收藏失败',
            icon: 'fail',
            duration: 2000
          })
        }
      }
    })
  },
  dianZan:function(formData){
    // console.log(JSON.stringify(formData))
    wx.request({
      url: getApp().data.server + '/index.php/Tongcheng/Book/dianZan', 
      data: {
         formData:JSON.stringify(formData),
         uid:getApp().globalData.userInfo.uid
      },
      header: {'content-type': 'application/x-www-form-urlencoded'},
      method:'POST',
      dataType:'json',
      success: function(res) {
        console.log(res.data)
        if(1 == res.data){
          wx.showToast({
            title:'点赞成功',
            icon: 'success',
            duration: 2000
          })
        }else if (2 == res.data){
          wx.showToast({
            title:'取消点赞',
            icon: 'success',
            duration: 2000
          })
        }else{
          wx.showToast({
            title:'点赞失败',
            icon: 'fail',
            duration: 2000
          })
        }
      }
    })
  },
  
  pinglun:function(formdata){
      console.log(formdata.pingLunContent)
      if(formdata.pingLunContent.length <= 0){
          wx.showToast({
              title:'请填写评论',
              icon: 'success',
              duration: 2000
          })
          return;
      }
    wx.request({
      url: getApp().data.server + '/index.php/Tongcheng/Book/pingLun', 
      data: {
         formData:JSON.stringify(formdata),
         uid:getApp().globalData.userInfo.uid
      },
      header: {'content-type': 'application/x-www-form-urlencoded'},
      method:'POST',
      dataType:'json',
      success: function(res) {
        // console.log(res.data)
        if(1 == res.data.code){
          getApp().globalData.iswangchengpinglun = 1;
          wx.showToast({
            title:'评论成功',
            icon: 'success',
            duration: 2000
          })
        }else if (2 == res.data.code){
          wx.showToast({
            title:'已评论',
            icon: 'fail',
            duration: 2000
          })
        }else{
          wx.showToast({
            title:'评论失败',
            icon: 'fail',
            duration: 2000
          })
        }
      }
    })
  },
  globalData:{
    userInfo:{},
    address:{},
    uploadimages:[],
    showPingLun:0,
    isWanChengUp:0,
    iswangchengpinglun:0,//用于判断是否已完成评论
    pushType:1,
    tabbar: {
      backgroundColor: "#fff",
      color: "#a2a2a2",
      selectedColor: "#f2914a",
      list: [
        {
          pagePath: "/pages/index/index",
          text: "闲置",
          iconPath: "/images/menu/ershou.png",
          selectedIconPath: "/images/menu/ershousl.png",
          width:"50rpx",
          height:"47rpx",
          show:'showindex'
        },
        {
          pagePath: "/pages/find/find",
          text: "发现",
          iconPath: "/images/menu/faxian.png",
          selectedIconPath: "/images/menu/faxiansl.png",
          width:"40rpx",
          height:"47rpx",
          show:'showfind'
        },
        {
          pagePath: "/pages/push/push",
          text: "上传",
          iconPath: "/images/menu/shangchuan.png",
          selectedIconPath: "/images/menu/shangchuan.png",
          width:"80rpx",
          height:"80rpx",
          show:'showup'

        },
        {
          pagePath: "/pages/community/community",
          text: "社区",
          iconPath: "/images/menu/luntan.png",
          selectedIconPath: "/images/menu/luntansl.png",
           width:"50rpx",
          height:"47rpx",
          show:'showshequ'
        },
        {
          pagePath: "/pages/my/my",
          text: "我的",
          iconPath: "/images/menu/geren.png",
          selectedIconPath: "/images/menu/gerensl.png",
          width:"50rpx",
          height:"47rpx",
          show:'showmy'
        }
      ]
    }
  }
})